package com.woyou.ui.activity.orderlist;

import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.MyOrderItem;
import com.woyou.utils.ParseUtils;

/**
 * @author blanks
 * @创建时间 2015年9月10日 下午2:23:20
 * @version v1.0 倒计时View
 */
public class WaitItemView extends BaseItemView {

	private TextView shopNames, summarys, allNums, allPrices, times;
	private ImageView rotateView;

	/**
	 * @param context
	 */
	public WaitItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		loadLayout();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.woyou.ui.adapter.packag.BaseItemView#loadLayout()
	 */
	@Override
	void loadLayout() {
		// TODO Auto-generated method stub
		LayoutInflater.from(getContext()).inflate(R.layout.item_myorder_waitacceptorder, this);
		shopNames = (TextView) findViewById(R.id.waitaccept_name);
		summarys = (TextView) findViewById(R.id.waitaccept_summary);
		allNums = (TextView) findViewById(R.id.waitaccept_allNum);
		allPrices = (TextView) findViewById(R.id.waitaccept_allMoney);
		rotateView = (ImageView) findViewById(R.id.waitaccept_iv);
		times = (TextView) findViewById(R.id.waitaccept_time);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.woyou.ui.adapter.packag.BaseItemView#loadData(com.woyou.bean.
	 * MyOrderItem)
	 */
	@Override
	public void loadData(MyOrderItem data) {
		// TODO Auto-generated method stub
		times.setTypeface(mTypeface);
		shopNames.setText(data.getsName());
		// holderWaitOrder.summary.setText(order.getSummary());

		allNums.setText(data.getNum() + "份");
		allPrices.setText("￥" + ParseUtils.String2Money(data.getoPrice()));
		if (!TextUtils.isEmpty(data.getSummary())) {
			summarys.setText(data.getSummary());
		} else {
			summarys.setText("");
		}
		
		waitOrder = data;
		
		// 物品东西 本地倒计时
		if (data.getRemainTime() > 0) {
			resetTextSize(times, data.getRemainTime());
			rotateView.setBackgroundResource(R.raw.order_countdown);
			orderCountDown(times, rotateView);
		} else {
			stopRotateAnim();
			rotateView.setBackgroundResource(R.raw.order_failure);
			times.setText("0");
		}
	}

	private void resetTextSize(final TextView timeTv, int remainTime) {
		float dimension = 0;
		if (remainTime > 99) {
			dimension = getContext().getResources().getDimension(R.dimen.sp100);
			timeTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, dimension);
		} else {
			dimension = getContext().getResources().getDimension(R.dimen.sp130);
			timeTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, dimension);
		}
	}

	RotateAnimation rotateAnimation;

	private void startRotateAnim(ImageView rotateImg) {
		// 创建一个RotateAnimation对象（以某个点为圆心旋转360度）相对于自己的360度旋转
		if (rotateAnimation == null) {
			rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
					0.5f);
			rotateAnimation.setDuration(1000);
			rotateAnimation.setRepeatCount(-1); // ndroid:repeatCount
												// 重复的次数，默认为0，必须是int，可以为-1表示不停止
			rotateAnimation.setInterpolator(new LinearInterpolator());
			rotateImg.setAnimation(rotateAnimation);
		}
		rotateAnimation.start();
	}

	private void stopRotateAnim() {
		if (rotateAnimation != null) {
			rotateAnimation.cancel();
		}
	}

	Timer countdownTimer = null;
	TimerTask countdownTask = null;
	MyOrderItem waitOrder = null;// 等待订单

	private void orderCountDown(final TextView timeTv, final ImageView imageView) {
		if (countdownTimer != null) {
			countdownTimer.cancel();
			countdownTimer = null;
		}

		if (countdownTask != null) {
			countdownTask.cancel();
			countdownTask = null;
		}

		countdownTimer = new Timer();
		countdownTask = new TimerTask() {
			@Override
			public void run() {
				runOnUI(new Runnable() {

					@SuppressLint("ResourceAsColor")
					@Override
					public void run() {

						if (waitOrder == null)
							return;

						startRotateAnim(imageView);

						if (waitOrder.getRemainTime() == 0) {
							countdownTimer.cancel();
							countdownTask.cancel();
							countdownTimer = null;
							countdownTask = null;
							orderListImp.refreshOrder();
							waitOrder.setRemainTime(-1);
							return;
						}
						if (waitOrder.getRemainTime() < 0) {
							stopRotateAnim();
							imageView.setBackgroundResource(R.raw.order_failure);
							timeTv.setText("0");
							orderListImp.refreshOrder();
							clear(); // 清除定时器
							return;
						}

						waitOrder.setRemainTime(waitOrder.getRemainTime() - 1);
						timeTv.setText(waitOrder.getRemainTime() + "");
						resetTextSize(timeTv, waitOrder.getRemainTime());
					}
				});
			}
		};
		countdownTimer.schedule(countdownTask, 0, 1000);

	}

	public void clear() {
		if (countdownTimer != null) {
			countdownTimer.cancel();
			countdownTimer = null;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
